package com.pig4cloud.pig.homecenter.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.pig4cloud.pig.homecenter.enums.TrueFalse;
import com.pig4cloud.pig.homecenter.json.CustomHassDateDeserializer;
import com.pig4cloud.pig.homecenter.json.CustomTrueFalseDeserializer;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 设备数据
 *
 * @author pig
 * @date 2025-01-07 15:06:18
 */
@Data
@TableName("hass_device_registry")
@EqualsAndHashCode(callSuper = true)
@Schema(description = "设备数据")
public class HassDeviceRegistryEntity extends Model<HassDeviceRegistryEntity> {


	/**
	 * devId
	 */
	@TableId(type = IdType.ASSIGN_ID)
	@Schema(description="devId")
	private Long devId;

	/**
	 * 区域
	 */
	@JsonAlias({"area_id", "areaId"})
	@Schema(description="区域")
	private String areaId;

	/**
	 * hass创建时间
	 */
	@JsonAlias({"created_at", "createdAt"})
	@JsonDeserialize(using = CustomHassDateDeserializer.class)
	@Schema(description="hass创建时间")
	private LocalDateTime createdAt;

	/**
	 * hass禁用情况
	 */
	@JsonAlias({"disabled_by", "disabledBy"})
	@Schema(description="hass禁用情况")
	private String disabledBy;

	/**
	 * 类型
	 */
	@JsonAlias({"entry_type", "entryType"})
	@Schema(description="类型")
	private String entryType;

	/**
	 * hwVersion
	 */
	@JsonAlias({"hw_version", "hwVersion"})
	@Schema(description="hwVersion")
	private String hwVersion;

	/**
	 * 设备编号
	 */
	@Schema(description="设备编号")
	private String id;

	/**
	 * 制造商
	 */
	@Schema(description="制造商")
	private String manufacturer;

	/**
	 * 型号
	 */
	@Schema(description="型号")
	private String model;

	/**
	 * 型号编号
	 */
	@JsonAlias({"model_id", "modelId"})
	@Schema(description="型号编号")
	private String modelId;

	/**
	 * hass更新时间
	 */
	@JsonAlias({"modified_at", "modifiedAt"})
	@JsonDeserialize(using = CustomHassDateDeserializer.class)
	@Schema(description="hass更新时间")
	private LocalDateTime modifiedAt;

	/**
	 * 命名人
	 */
	@JsonAlias({"name_by_user", "nameByUser"})
	@Schema(description="命名人")
	private String nameByUser;

	/**
	 * 名称
	 */
	@Schema(description="名称")
	private String name;

	/**
	 * 自定义名称
	 */
	@JsonAlias({"custom_name", "customName"})
	@Schema(description="自定义名称")
	private String customName;

	/**
	 * primaryConfigEntry
	 */
	@JsonAlias({"primary_config_entry", "primaryConfigEntry"})
	@Schema(description="primaryConfigEntry")
	private String primaryConfigEntry;

	/**
	 * 序列号
	 */
	@JsonAlias({"serial_number", "serialNumber"})
	@Schema(description="序列号")
	private String serialNumber;

	/**
	 * 软件版本
	 */
	@JsonAlias({"sw_version", "swVersion"})
	@Schema(description="软件版本")
	private String swVersion;

	/**
	 * viaDeviceId
	 */
	@JsonAlias({"via_device_id", "viaDeviceId"})
	@Schema(description="viaDeviceId")
	private String viaDeviceId;

	/**
	 * 关联实体
	 */
	@JsonDeserialize(using = HassDeviceRegistryEntity.CustomAny2StrDeserializer.class)
	@Schema(description="关联实体")
	private String identifiers;

	/**
	 * 用户
	 */
	@Schema(description="用户")
	private Long userId;

	/**
	 * 平台更新时间
	 */
	@TableField(fill = FieldFill.INSERT_UPDATE)
	@Schema(description="平台更新时间")
	private LocalDateTime updateTime;

	@Schema(description="是否可见")
	private Boolean visible;

	@TableField(exist = false)
	private String areaName;

	public static class CustomAny2StrDeserializer extends JsonDeserializer<String> {

		@Override
		public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
			JsonNode node = p.getCodec().readTree(p);

			return node.toString();
		}
	}


}
